package com.mathplayer.js;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.dom.client.Element;
import com.google.gwt.user.client.ui.RootPanel;
import com.mathplayer.player.MathManager;
import com.mathplayer.player.interaction.InteractionManager;

public class MathPlayerController {
	private JavaScriptObject jsObject;
	private InteractionManager interactionManager;
	
	public MathPlayerController(String id, String expression){
		jsObject = JavaScriptObject.createFunction();
		initJavaScriptObject(jsObject);

		MathManager mm = new MathManager();
		interactionManager = mm.createMath(expression, RootPanel.get(id));

	}
	
	public native void initJavaScriptObject(JavaScriptObject jsObject)/*-{
		var instance = this;
		jsObject.getGapTextAt = function(index){
			return instance.@com.mathplayer.js.MathPlayerController::getGapTextAt(I)(index);
		}
		jsObject.setGapTextAt = function(index, text){
			instance.@com.mathplayer.js.MathPlayerController::setGapTextAt(ILjava/lang/String;)(index, text);
		}
		jsObject.getGapsCount = function(){
			return instance.@com.mathplayer.js.MathPlayerController::getGapsCount()();
		}
		jsObject.getGapElementAt = function(index){
			return instance.@com.mathplayer.js.MathPlayerController::getGapElementAt(I)(index);
		}
	}-*/;
	
	public JavaScriptObject getJavaScriptObject(){
		return jsObject;
	}

	public String getGapTextAt(int index){
		return interactionManager.getGapAt(index).getText();
	}
	
	public void setGapTextAt(int index, String text){
		interactionManager.getGapAt(index).setText(text);
	}
	public int getGapsCount(){
		return interactionManager.getGapsCount();
	}
	public Element getGapElementAt(int index){
		return interactionManager.getGapAt(index).getElement();
	}
}
